package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


public class ChangPwdActivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private Button find_code;
    private ImageView regist_back;
    private TextView tv_submit,tv_headtitle;
    private String phone,password;
    private EditText sms_code, new_password, ok_edit;
    private Context context;
    private TimeCount registerCheck;
    private ContributorPresenter contributorPresenter;
    private final String USER_FIND_PWD="USER_FIND_PWD";
    private final String USER_JUDGE_VCODE="USER_JUDGE_VCODE";
    private final String USER_UPDATE_PWD="USER_UPDATE_PWD";
    private MyToast myToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.changpwd_layout);
        context=this;
        myToast=new MyToast(context);
        phone= Tools.getSharedPreferencesStrings(Constant.SharePreferencePhone,"",context);
        registerCheck = new TimeCount(60000, 1000);
        contributorPresenter = new ContributorPresenter(this, this);
        initView();
    }

    private void initView() {
        find_code = (Button) findViewById(R.id.find_code);
        regist_back = (ImageView) findViewById(R.id.regist_back);
        tv_submit = (TextView) findViewById(R.id.tv_submit);
        sms_code = (EditText) findViewById(R.id.sms_code);
        new_password = (EditText) findViewById(R.id.new_password);
        ok_edit = (EditText) findViewById(R.id.ok_edit);
        tv_submit.setText("提交");
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("修改密码");
        find_code.setOnClickListener(this);
        regist_back.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
    }
    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(USER_FIND_PWD)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10400"))
                {
                    myToast.showShort("用户不存在");
                }else if(code.equals("10600"))
                {
                    myToast.showShort("验证码发送失败");
                }else if(code.equals("10601"))
                {
                    myToast.showShort("验证码发送频繁");
                }
                else if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("21"))
                        myToast.showShort("验证码发送正确");
                }
            }else if(flag.equals(USER_JUDGE_VCODE))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10602"))
                {
                    myToast.showShort("验证码失效");
                }else if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("22"))
                    {
                        contributorPresenter.getUserUpdatePwd(phone,password,USER_UPDATE_PWD);
                    }
                }
            }
            else if(flag.equals(USER_UPDATE_PWD))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10800"))
                {
                    myToast.showShort("设置密码失败");
                }else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    if(message.equals("31"))
                    {
                        myToast.showShort("设置密码成功");
                        finish();
                    }
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.find_code:
                registerCheck.start();
                contributorPresenter.getUserSendVcode(phone,1,USER_FIND_PWD);
                break;
            case R.id.tv_submit:
                String vcode=sms_code.getText().toString().trim();
                password=new_password.getText().toString().trim();
                String password2=ok_edit.getText().toString().trim();

                if (password.isEmpty() || password2.isEmpty() || vcode.isEmpty()) {
                   if (vcode.isEmpty()){
                       myToast.showShort("短信验证码不能为空");
                       return;
                   }
                   if (password.isEmpty()){
                        myToast.showShort("密码不能为空");
                        return;
                    }
                    if (password2.isEmpty()){
                        myToast.showShort("确认密码不能为空");
                        return;
                    }
                } else {
                    if(password.equals(password2))
                    {
                        contributorPresenter.getUserJudgeVcode(phone,vcode,USER_JUDGE_VCODE);
                    }else
                    {
                        myToast.showShort("密码不一致");
                    }
                }
                break;
        }
    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            find_code.setText("重获验证码");
            setSendRegisterCodeEnable();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            find_code.setText(millisUntilFinished / 1000 + "秒后发送");
            setSendRegisterCodeUnenable();
        }
    }
    private void setSendRegisterCodeEnable() {
        find_code.setClickable(true);
        find_code.setBackgroundResource(R.drawable.ok_shape);
    }
    private void setSendRegisterCodeUnenable() {
        find_code.setClickable(false);
        find_code.setBackgroundResource(R.drawable.ok_shape_click);
    }
}
